﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;

namespace UI
{
    public class EquipIcon : MonoBehaviour
    {
        public TextureKit equipIcon;
        public UILabel equipLevel;
        public UILabel labNum;
        public GameObject firstFlag; //首次掉落标记
        public GameObject chosedBg;
        public GameObject selectMark;
        public GameObject levelBg;
        public UIEquipment m_equip;
        public UITreasure m_treasure;
        public GameObject fragFrame;
        public UILabel fragNum;
        public GameObject onArrayMark;
        public GameObject widgetForBag;
        public GameObject imgJiban;
        public GameObject redPoint;
        public GameObject gemRoot;      //镶嵌宝石根节点
        public GameObject gemPref;

        public string guid;
        public delegate void ClickCallBack(EquipIcon obj);
        ClickCallBack m_callBack;

        // 点击时是否显示选中效果
        bool m_withChoseBg;

        public bool showDescription = false;

        void Awake()
        {
            chosedBg.SetActive(false);
            firstFlag.SetActive( false );
            onArrayMark.SetActive(false);
            m_withChoseBg = false;
        }

        public void SetEquip(UIEquipment equip)
        {
            guid = equip.guid;
            m_equip = equip;
            m_treasure = null;
            equipIcon.iconType = TextureIcon.EQUIP;
            equipIcon.textureName = equip.equipData.icon;
            string heroId = "";
            if (equip.owner != null)
            {
                heroId = equip.owner.hero.heroId;
            }
            else if (equip.lastHeroId != "")
            {
                heroId = equip.lastHeroId;
            }

            if (heroId != "")
            {
                List<TableWeaponRes> weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroId, equip.equipId);
                if (weapons.Count > 0 && weapons[0].iconRes != "")
                {
                    equipIcon.textureName = weapons[0].iconRes;
                }
            }

            SetLevelBg(equip.equipData.quality);

            equipLevel.text = equip.level.ToString();

            fragFrame.SetActive(false);

            UpdateGem();
        }

        //专属武器
        public void SetEquip(UIEquipment equip,string heroId)
        {
            List<TableWeaponRes>  weapons = Dict.DictDataManager.Instance.tableWeaponResDao.GetByTwoIndex(heroId,equip.equipId);


            if (weapons.Count > 0)
            {
                guid = equip.guid;
                m_equip = equip;
                m_treasure = null;
                equipIcon.iconType = TextureIcon.EQUIP;

                if(weapons[0].iconRes == "")
                    equipIcon.textureName = equip.equipData.icon;
                else
                    equipIcon.textureName = weapons[0].iconRes;

                SetLevelBg(equip.equipData.quality);

                equipLevel.text = equip.level.ToString();

                fragFrame.SetActive(false);
            }
            else
            {
                SetEquip(equip);
            }
        }

        public void SetEquipWithId(string id)
        {
            TableEquip equipData = DictEquipBlo.GetTableEquip(id);

            if (equipData == null)
                return;

            equipIcon.iconType = TextureIcon.EQUIP;
            equipIcon.textureName = equipData.icon;

            SetLevelBg(equipData.quality);

            levelBg.SetActive(false);
            fragFrame.SetActive(false);
        }

        public void SetTreasure(UITreasure trea)
        {
            guid = trea.guid;
            m_treasure = trea;
            m_equip = null;
            equipIcon.iconType = TextureIcon.TREASURE;
            equipIcon.textureName = trea.treasureData.icon;

            SetLevelBg(trea.treasureData.quality);

            equipLevel.text = trea.strengthLevel.ToString();

            fragFrame.SetActive(false);
        }

        public void SetTreasureWithId(string id)
        {
            TableTreasure treasureData = DictTreasureBlo.GetTableTreasure(id);

            if (treasureData == null)
                return;

            equipIcon.iconType = TextureIcon.TREASURE;
            equipIcon.textureName = treasureData.icon;

            SetLevelBg(treasureData.quality);

            levelBg.SetActive(false);
            fragFrame.SetActive(false);
        }
        
        public void SetFrag(UIFragItem frag)
        {
            guid = frag.fragData.id;
            int quality = 0;
            if(guid.StartsWith(Dict.Config.TableConst.frag_equip))
            {
                TableEquip equipData = DictEquipBlo.GetTableEquip(frag.fragData.target);
                equipIcon.iconType = TextureIcon.EQUIP;
                equipIcon.textureName = equipData.icon;
                quality = equipData.quality;
            }
            else if (guid.StartsWith(Dict.Config.TableConst.frag_trea))
            {
                TableTreasure treasureData = DictTreasureBlo.GetTableTreasure(frag.fragData.target);
                equipIcon.iconType = TextureIcon.TREASURE;
                equipIcon.textureName = treasureData.icon;
                quality = treasureData.quality;
            }

            SetLevelBg(quality);
            levelBg.SetActive(false);
            fragFrame.SetActive(true);
            fragNum.text = frag.num.ToString();
        }
        public void SetFragWithId(string id)
        {
            var tf = DictFragBlo.GetTableFrag(id);
            if(tf == null)
            {
                Debug.LogError("frag id " + id + " not exist!");
                return;
            }
            UIFragItem frag = new UIFragItem(tf);
            SetFrag(frag);
        }

		public void SetTreasureFrag(TableTreasure treasureData, int number)
        {
            guid = treasureData.id;
            equipIcon.iconType = TextureIcon.TREASURE;
            equipIcon.textureName = treasureData.icon;
            SetLevelBg(treasureData.quality);
            levelBg.SetActive(false);
            fragFrame.SetActive(true);
            fragNum.text = number.ToString();
        }

        public void SetLight(bool light)
        {
            if(light)
            {
                equipIcon.texture.color = Color.white;
                fragFrame.GetComponent<UISprite>().color = Color.white;
            }
            else
            {
                equipIcon.texture.color = new Color(0.5f,0.5f,0.5f);
                fragFrame.GetComponent<UISprite>().color = new Color(0.5f, 0.5f, 0.5f);
            }
        }

        public long UpdateFragNum(int num = 0)
        {
            if(num <= 0)
            {
                if (guid.StartsWith(Dict.Config.TableConst.frag_equip))
                {
                    long count = StaticData.playerData.GetFragEquipments(guid).num;
                    fragNum.text = count.ToString();
                    return count;
                }
                else if (guid.StartsWith(Dict.Config.TableConst.frag_trea))
                {
                    long count = StaticData.playerData.treasureFrag.GetFrag(guid).num;
                    fragNum.text = count.ToString();
                    return count;
                }
            }
            else
            {
                fragNum.text = num.ToString();
            }
            return num;
        }

        public void UpdateRedPointForFrag(UIFragItem frag)
        {
            if (frag.num >= frag.fragData.num)
                redPoint.SetActive(true);
            else
                redPoint.SetActive(false);
        }

        //一般用于显示经验卡个数
        public void UpdateItemCount(string text, UIWidget.Pivot pivot = UIWidget.Pivot.Center)
        {
            labNum.gameObject.SetActive(true);
            labNum.pivot = pivot;
            labNum.text = text;
        }

        public void SetOnArray()
        {
            onArrayMark.SetActive(true);
        }

        public void UpdateGem()
        {
            if (m_equip == null)
            {
                gemRoot.SetActive(false);
                return;
            }
            
            List<int> gemColors = new List<int>();
            if(m_equip.equipData.hole1 != 0)
            {
                gemColors.Add(m_equip.equipData.hole1);
            }
            if (m_equip.equipData.hole2 != 0)
            {
                gemColors.Add(m_equip.equipData.hole2);
            }
            if (m_equip.equipData.hole3 != 0)
            {
                gemColors.Add(m_equip.equipData.hole3);
            }
            if (m_equip.equipData.hole4 != 0)
            {
                gemColors.Add(m_equip.equipData.hole4);
            }

            if(gemColors.Count == 0)
            {
                gemRoot.SetActive(false);
                return;
            }

            //if(m_equip.gems.Count == 0 && m_equip.owner == null)
            //{
            //    gemRoot.SetActive(false);
            //    return;
            //}

            int countOf7 = 0;   //七彩宝石数
            Dictionary<int, string> haveGems = new Dictionary<int, string>();
            foreach(var item in m_equip.gems)
            {
                TableMineSkill minedata = DictMineBlo.GetTableMineSkill(item);
                if(minedata != null)
                {
                    haveGems[minedata.Color] = item;
                    if (minedata.Color == 7)
                        countOf7++;
                }
            }

            gemRoot.transform.DestroyChildren();
            gemRoot.SetActive(true);
            int startX = (gemColors.Count - 1) * -10;
            for(int i = 0; i < gemColors.Count; i++)
            {
                GameObject gem = Instantiate(gemPref);
                gem.SetActive(true);
                gem.transform.parent = gemRoot.transform;
                gem.transform.localScale = Vector3.one;
                gem.transform.localPosition = new Vector3(startX, 0, 0);
                startX += 20;

                bool haveGem = false;
                if (i < m_equip.gems.Count)
                {
                    TableMineSkill minedata = DictMineBlo.GetTableMineSkill(m_equip.gems[i]);
                    if (minedata != null)
                    {
                        haveGem = true;
                        switch (minedata.Color)
                        {
                            case (int)GemColorType.RED:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_red";
                                break;
                            case (int)GemColorType.PURPLE:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_purple";
                                break;
                            case (int)GemColorType.GREEN:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_green";
                                break;
                            case (int)GemColorType.BLUE:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_blue";
                                break;
                            case (int)GemColorType.YELLOW:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_yellow";
                                break;
                            case (int)GemColorType.WHITE:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_white";
                                break;
                            case (int)GemColorType.COLORFUL:
                                gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_qicai";
                                break;
                        }
                    }
                }

                if (!haveGem)
                {
                    switch (gemColors[i])
                    {
                        case (int)GemColorType.RED:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_red_b";
                            break;
                        case (int)GemColorType.PURPLE:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_purple_b";
                            break;
                        case (int)GemColorType.GREEN:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_green_b";
                            break;
                        case (int)GemColorType.BLUE:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_blue_b";
                            break;
                        case (int)GemColorType.YELLOW:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_yellow_b";
                            break;
                        case (int)GemColorType.WHITE:
                            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_white_b";
                            break;
                    }
                }

                //if(haveGems.ContainsKey(gemColors[i]))
                //{
                //    switch(gemColors[i])
                //    {
                //        case (int)GemColorType.RED:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_red";
                //            break;
                //        case (int)GemColorType.PURPLE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_purple";
                //            break;
                //        case (int)GemColorType.GREEN:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_green";
                //            break;
                //        case (int)GemColorType.BLUE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_blue";
                //            break;
                //        case (int)GemColorType.YELLOW:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_yellow";
                //            break;
                //        case (int)GemColorType.WHITE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_white";
                //            break;
                //    }
                //}
                //else if(countOf7 > 0)
                //{
                //    gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_qicai";
                //    countOf7--;
                //}
                //else
                //{
                //    switch (gemColors[i])
                //    {
                //        case (int)GemColorType.RED:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_red_b";
                //            break;
                //        case (int)GemColorType.PURPLE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_purple_b";
                //            break;
                //        case (int)GemColorType.GREEN:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_green_b";
                //            break;
                //        case (int)GemColorType.BLUE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_blue_b";
                //            break;
                //        case (int)GemColorType.YELLOW:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_yellow_b";
                //            break;
                //        case (int)GemColorType.WHITE:
                //            gem.GetComponent<UISprite>().spriteName = "UI_yelianwu_icon_baoshi_white_b";
                //            break;
                //    }
                //}
            }
        }

        void SetLevelBg(int quality)
        {
            switch ((QualityValue)quality)
            {
                case QualityValue.WHITE:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_white";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_white";
                    }
                    break;
                case QualityValue.GREEN:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_green";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_green";
                    }
                    break;
                case QualityValue.BLUE:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_blue";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_blue";
                    }
                    break;
                case QualityValue.PURPLE:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_zi";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_zi";
                    }
                    break;
                case QualityValue.YELLOW:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_yellow";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_yellow";
                    }
                    break;
                case QualityValue.RED:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_red";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_red";
                    }
                    break;
                default:
                    {
                        levelBg.GetComponent<UISprite>().spriteName = "UI_kuang_lv_bi_white";
                        fragFrame.GetComponent<UISprite>().spriteName = "UI_kuang_sui_white";
                    }
                    break;
            }
        }

        public void SetWithChoseBg(bool with)
        {
            m_withChoseBg = with;
        }

        public void UpdateLevel()
        {
            if(m_treasure != null)
            {
                m_treasure = StaticData.playerData.GetUITreasure(m_treasure.guid);
                equipLevel.text = m_treasure.strengthLevel.ToString();
            }
            if(m_equip != null)
            {
                m_equip = StaticData.playerData.GetUIEquipment(m_equip.guid);
                equipLevel.text = m_equip.level.ToString();
            }
        }

        public void OnClick()
        {
            if(m_withChoseBg)
                chosedBg.SetActive(true);
            
            if(m_callBack != null)
            {
                m_callBack(this);
            }

            //if(showDescription)
            //{
            //    ShowDescription();
            //}
        }

        public void SetChosedCallback(ClickCallBack callback)
        {
            m_callBack = callback;
        }

        public ClickCallBack GetChosedCallback()
        {
            return m_callBack;
        }

    }
}
